package com.ljy.my_study.java.thread.interrupt;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author lijunying
 * @date 2021/1/27 9:04
 */
public class TestMain {

    public static void main(String[] args) throws InterruptedException {
//        test1();
//        test2();
        test3();
//      test4();
    }

    public static void test1(){
        Runnable runnable=()->{
            try {
                System.out.println(Thread.currentThread().getName()+" start...");
                Thread.sleep(2*1000L);
                System.out.println(Thread.currentThread().getName()+" end...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        new Thread(runnable).start();
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(Thread.interrupted()){
            System.out.println("线程打断");
        }
    }

    /**
     * 只有处于WAITING或TIME_WAITING状态才能被打断。需要调用线程相关方法
     * 当线程中方法抛出InterruptedException时，才能被打断
     */
    public static void test2(){
        Runnable runnable=()->{
            try {
                System.out.println(Thread.currentThread().getName()+" start...");
                Thread.interrupted();
                Thread.sleep(2*1000L);
                Thread.interrupted();
                System.out.println(Thread.currentThread().getName()+" end...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        Thread thread1=new Thread(runnable);
        thread1.start();
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束");
    }

    public static Runnable getRunnable() throws InterruptedException{
        return ()->{
            for(int i=0;i<1000000;i++){
                try {
                    System.out.println(Thread.currentThread().getName()+" start...");
                    Thread.sleep(2*1000L);
                    System.out.println(Thread.currentThread().getName()+" end...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public static void test3(){

        Thread thread1= null;
        try {
            thread1 = new Thread(getRunnable());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread1.start();
//        try {
//            Thread.sleep(1000L);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        thread1.interrupt();
        System.out.println("线程打断");
    }

    /**
     * 当调用interrupt后，interrupted才变成true，否者都是false
     *
     * interrupt()意思是通知线程应该中断了
     * @throws InterruptedException
     */
    public static void test4() throws InterruptedException{
        System.out.println("首次调用thread.interrupted()："
                +Thread.currentThread().interrupted());
        Thread.currentThread().interrupt();
        System.out.println("打断之后");
        System.out.println("第一次调用Thread.currentThread().interrupt()："
                +Thread.currentThread().isInterrupted());
        System.out.println("第一次调用thread.interrupted()："
                +Thread.currentThread().interrupted());
        System.out.println("第二次调用thread.interrupted()："
                +Thread.currentThread().interrupted());

    }

}
